<?php
App::uses('AppModel', 'Model');
/**
 * Devi Model
 *
 * @property utilisateurs $utilisateurs
 * @property societe $societe
 */
class Devi extends AppModel {
	
	/*public function modifierLaRemise($remise, $id_devis) {
		$this->query("UPDATE devis SET remise_devis = 0$remise WHERE id_devis = $id_devis");
	}*/
	
	public function modifierLaRef($ref, $id_devis) {
		$this->query("UPDATE devis SET ref_devis='".$ref."' WHERE id_devis =".$id_devis);
	}
	
	public function vider($id) {
		$this->query("DELETE FROM ligne_devis WHERE id_devis = $id");
	}
	
	// récupère les données de chaque articles se trouvant dans les réponses données du questionnaire
	// paramètre : les réponses données dans le questionnaire
	public function recupArticlesDepuisReponses($id_questionnaire) {
		$result = Array();
		$articles = $this->query("SELECT * FROM reponses_donnees ".
								"JOIN articles article ON article.id_article=reponses_donnees.id_article ".
								"JOIN groupe_devis groupe ON groupe.id_groupe_devis = article.id_groupe_devis ".
								"JOIN bloc_devis bloc ON bloc.id_bloc_devis = groupe.id_bloc_devis ".
								"WHERE reponses_donnees.id_questionnaire=$id_questionnaire");
		
		foreach($articles as $a) {
			$a['quantite'] = $a['reponses_donnees']['reponse_quantite_article'];
			$result[] = $a;
		}
		return $result;
	}
	
	public function infosSocieteDevis($id_devis) {
		$q = $this->query("SELECT * FROM devis ".
							"JOIN societes as Societe ON Societe.id_societe=devis.id_societe ".
							"JOIN questionnaires as questionnaire ON questionnaire.id_societe=Societe.id_societe ".
							"JOIN utilisateurs as utilisateur ON utilisateur.id_utilisateur=devis.id_utilisateur ".
							"WHERE id_devis=$id_devis");
		return $q[0];
	}
	
	public function infos_pour_dupliquer($id_devis) {
		$q = $this->query("SELECT * FROM devis AS devis ".
							"JOIN societes AS societe ON societe.id_societe=devis.id_societe ".
							"JOIN sites AS site ON site.id_societe=societe.id_societe ".
							"JOIN questionnaires AS questionnaire ON questionnaire.id_societe=Societe.id_societe ".
							"JOIN utilisateurs AS utilisateur ON utilisateur.id_utilisateur=devis.id_utilisateur ".
							"WHERE id_devis=$id_devis");
		$res = array();
		$res['devis'] = $q[0]['devis'];
		$res['societe'] = $q[0]['societe'];
		$res['utilisateur'] = $q[0]['utilisateur'];
		$res['questionnaire'] = $q[0]['questionnaire'];
		foreach($q as $infos) {
			$res['sites'][] = $infos['site'];
		}
		return $res;
	}


/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_devis';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_devis' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'statut_devis' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'date_creation' => array(
			'datetime' => array(
				'rule' => array('datetime'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'date_modification' => array(
			'datetime' => array(
				'rule' => array('datetime'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'id_utilisateur' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'id_societe' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
	public $hasOne = array(
		'utilisateurs' => array(
			'className' => 'utilisateurs',
			'foreignKey' => 'id_utilisateur',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'societe' => array(
			'className' => 'societe',
			'foreignKey' => 'id_societe',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
}
